﻿using System;
using System.Collections.Generic;
using Entidades;
using Controladores;
using Web.Helpers.jqGridHelpers;
using Common;

namespace Web.Helpers
{
    public class PagedData<T> where T : Entidades.Entidad
    {
        #region Properties

        /// <summary>
        /// Total Records
        /// </summary>
        public int TotalRecords { get; set;  }

        /// <summary>
        /// Total Pages
        /// </summary>
        public int TotalPages { get; set;  }
     
        /// <summary>
        /// Current Page
        /// </summary>
        public int CurrentPage { get; set;  }

        /// <summary>
        /// Data
        /// </summary>
        public List<T> Data { get; set;  }

        #endregion
    }

    public class GridDataHelper
    {
        #region Public Methods
        public static Common.Data.ParameterList GetParameters(string searchFilter, string colModel)
        {
            var parameterList   = new Common.Data.ParameterList();
            var jqSearchFilter  = new jqGridSearchFilterBuilder(searchFilter, colModel);
            foreach(var ruleSet in jqSearchFilter.RuleSets)
            {
                foreach (var rule in ruleSet.Rules)
                {
                    var field       = rule.Field;
                    var data        = rule.Data;
                    var columnType  = jqSearchFilter.ColumnsInfo[field];
                    var value       = ConvertValue(columnType, data);

                    parameterList.AddParameter(field, value);
                }
            }
            return parameterList;
        }
        public static PagedData<Entidades.Entidad> GetPaginationData(int totalRecords, int currentPage, int pageSize)
        {
            return GridDataHelper<Entidades.Entidad>.GetGridData(totalRecords, currentPage, pageSize);
        }
        #endregion

        #region Private Methods
        private static object ConvertValue(Type columnType, string data)
        {
            if (columnType == typeof(Boolean))
                //return Convert.ToBoolean(data.ToLower());
                return Common.Formatter.ToBoolean(data.ToLower());

            if (columnType == typeof(Int32))
                return Convert.ToInt32(data);

            if (columnType == typeof(Int64))
                return Convert.ToInt64(data);

            if (columnType == typeof(Decimal))
                return Convert.ToDecimal(data);

            if (columnType == typeof(Double))
                return Convert.ToDouble(data);

            if (columnType == typeof(Guid))
                return new Guid(data);

            if (columnType == typeof(DateTime))
                return Convert.ToDateTime(data);

            if (columnType == typeof(String))
                return data;

            return null;
        }
        #endregion
    }

    public class GridDataHelper<T> where T : Entidades.Entidad
    {
        #region Public Methods
        public PagedData<T> GetData(string searchFilter, string colModel, int currentPage, int pageSize, string sortOrder, string sortColumn)
        {
            var controller      = new ControladorBase<T>();
            var filter          = new jqGridSearchFilterBuilder(searchFilter, colModel).ToQuery();
            var totalRecords    = controller.ObtenerTotalEntidades(filter);
            var totalPages      = (int)Math.Ceiling((decimal)totalRecords / (decimal)pageSize);
            var pagination      = new Data.Pagination(totalRecords, totalPages, currentPage, pageSize, sortOrder, sortColumn);
            var records         = controller.ObtenerEntidades(pagination, filter);
            
            return GridDataHelper<T>.GetGridData(totalRecords, currentPage, pageSize, records);
        }
        public static PagedData<T> GetGridData(int totalRecords, int currentPage, int pageSize, List<T> data = null)
        {
            return new PagedData<T>
            {
                TotalRecords = totalRecords,
                TotalPages = (int) Math.Ceiling((decimal) totalRecords/(decimal) pageSize),
                CurrentPage = currentPage,
                Data = data
            };
        }
        #endregion
    }
}